<template>
  <div>
    <el-form>
      <el-form-item
        v-for="(item, index) in objList"
        :key="index"
        :label="item.description"
      >
        <el-checkbox-group v-model="selfObjs[item.key]['valueList']">
          <el-checkbox v-for="(cb, i) in fullObjs[item.key]['valueList']" :key="i" :label="cb" />
        </el-checkbox-group>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getAllObjs, addObjAuthority, getObjByAutorityId } from '@/api/sysObj'
export default {
  name: 'Objs',
  props: {
    row: {
      default: function() {
        return {}
      },
      type: Object
    }
  },
  data() {
    return {
      fullObjs: {},
      selfObjs: {},
      objList: []
    }
  },
  async created() {
    // 获取全量对象
    const res2 = await getAllObjs({ parentId: this.row.parentId })
    this.fullObjs = this.buildObjMap(res2.data.objs)
    // 获取已有的对象
    const res = await getObjByAutorityId({
      authorityId: this.row.authorityId
    })
    this.selfObjs = this.buildObjMap(res.data.objs)
    this.objList = res2.data.objs
    this.activeUserId = this.row.authorityId
  },
  methods: {

    // 暴露给外层使用的切换拦截统一方法
    enterAndNext() {
      this.authObjEnter()
    },
    // 创建button树方法
    buildObjMap(objs) {
      var objMap = {}
      objs && objs.map(item => {
        Object.assign(objMap, { [item.key]: item })
      })
      return objMap
    },
    // 关联关系确定
    async authObjEnter() {
      const res = await addObjAuthority({
        authorityId: this.activeUserId,
        objs: this.selfObjs
      })
      if (res.code === 0) {
        this.$notify({
          type: 'success',
          title: '成功',
          message: '对象设置成功'
        })
      }
    }
  }
}
</script>
<style lang="scss">
</style>
